/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.pojo.sqlServer.PPMoveMissionHistory;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.PPMoveMissionHistoryRepository;
import me.zhengjie.service.PPMoveMissionHistoryService;
import me.zhengjie.service.dto.PPMoveMissionHistoryDto;
import me.zhengjie.service.dto.PPMoveMissionHistoryQueryCriteria;
import me.zhengjie.service.mapper.PPMoveMissionHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-03
**/
@Service
@RequiredArgsConstructor
public class PPMoveMissionHistoryServiceImpl implements PPMoveMissionHistoryService {

    private final PPMoveMissionHistoryRepository ppMoveMissionHistoryRepository;
    private final PPMoveMissionHistoryMapper ppMoveMissionHistoryMapper;

    @Override
    public PageResult<PPMoveMissionHistoryDto> queryAll(PPMoveMissionHistoryQueryCriteria criteria, Pageable pageable){
//        String createTime=null;
//        String finishTime=null;;
//        if(criteria.getCreateTime()!=null){
//            createTime = criteria.getCreateTime();
//            int indexOfT = createTime.indexOf("T"); // 找到T的位置
//            createTime = createTime.substring(0, indexOfT); // 截取T前面的部分
//        }
//
//        if(criteria.getFinishTime()!=null){
//            finishTime = criteria.getFinishTime();
//            int indexOfT = finishTime.indexOf("T"); // 找到T的位置
//            finishTime = finishTime.substring(0, indexOfT); // 截取T前面的部分
//        }
//
//        criteria.setCreateTime(createTime);
//        criteria.setFinishTime(finishTime);

        Page<PPMoveMissionHistory> page = ppMoveMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppMoveMissionHistoryMapper::toDto));
    }

    @Override
    public List<PPMoveMissionHistoryDto> queryAll(PPMoveMissionHistoryQueryCriteria criteria){
        return ppMoveMissionHistoryMapper.toDto(ppMoveMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPMoveMissionHistoryDto findById(Integer id) {
        PPMoveMissionHistory ppMoveMissionHistory = ppMoveMissionHistoryRepository.findById(id).orElseGet(PPMoveMissionHistory::new);
        ValidationUtil.isNull(ppMoveMissionHistory.getId(),"PpMoveMissionHistory","id",id);
        return ppMoveMissionHistoryMapper.toDto(ppMoveMissionHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPMoveMissionHistory resources) {
        ppMoveMissionHistoryRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPMoveMissionHistory resources) {
        PPMoveMissionHistory ppMoveMissionHistory = ppMoveMissionHistoryRepository.findById(resources.getId()).orElseGet(PPMoveMissionHistory::new);
        ValidationUtil.isNull( ppMoveMissionHistory.getId(),"PpMoveMissionHistory","id",resources.getId());
        ppMoveMissionHistory.copy(resources);
        ppMoveMissionHistoryRepository.save(ppMoveMissionHistory);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppMoveMissionHistoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPMoveMissionHistoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPMoveMissionHistoryDto ppMoveMissionHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务编码", ppMoveMissionHistory.getMissionCode());
            map.put("载盘编码", ppMoveMissionHistory.getPalletCode());
            map.put("任务状态", ppMoveMissionHistory.getMissionStatus());
            map.put("起点", ppMoveMissionHistory.getSource());
            map.put("终点", ppMoveMissionHistory.getDestination());
            map.put("创建时间", ppMoveMissionHistory.getCreateTime());
            map.put("开始时间", ppMoveMissionHistory.getStartTime());
            map.put("结束时间", ppMoveMissionHistory.getFinishTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}